package com.fengye.rabbit.login.message;

import com.fengye.rabbit.login.dto.UserLoginDTO;
import com.fengye.rabbit.login.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * 接收消息，记录日志
 * @author sky
 * @date 2021年06月09日 3:55 下午
 */
@Component
@Slf4j
public class LogConsumer {

    @Autowired
    SysLogService sysLogService;

    @RabbitListener(queues = "${login.message.queue.name:login-queue-name}", containerFactory = "singleListenerContainer")
    public void consumeLoginMsg(@Payload UserLoginDTO dto) {
        try {
            log.info("接收到日志消息,{}", dto);
            sysLogService.recordLog(dto);
        } catch (Exception e) {
            log.error("接收到日志消息,{},记录日志异常", dto, e.fillInStackTrace());
        }
    }
}
